home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / Xconq 7.0d16 / Xconq 7.0d16 src / Tools / imfam.c next >
Encoding:
Text File  |  1993-11-22  |  49.3 KB  |  191 lines  |  [TEXT/KAHL]

  1.  NULL;
  2.         handle = GetIndResource(typ, i + 1);
  3.         GetResInfo(handle, &resid, &restype, resname);
  4.         if (resname[0] > 0) {
  5.             resname[resname[0]+1] = '\0';
  6.             imfname = copy_string(resname+1);
  7.         } else {
  8.             /* (should warn about unnamed resources being ignored?) */
  9.         }
  10.         if (imfname != NULL) {
  11.             get_imf(imfname);
  12.         }
  13.     }
  14. }
  15.  
  16. collect_all_colors(typ)
  17. ResType typ;
  18. {
  19.     int i, n;
  20.     char *imcname;
  21.     Handle handle;
  22.     short resid;  ResType restype;  Str255 resname;
  23.  
  24.     n = CountResources(typ);
  25.     for (i = 0; i < n; ++i ) {
  26.         imcname = NULL;
  27.         handle = GetIndResource(typ, i + 1);
  28.         GetResInfo(handle, &resid, &restype, resname);
  29.         if (resname[0] > 0) {
  30.             resname[resname[0]+1] = '\0';
  31.             imcname = copy_string(resname+1);
  32.         } else {
  33.             /* (should warn about unnamed resources being ignored?) */
  34.         }
  35.         if (imcname != NULL) {
  36.             get_imc(imcname);
  37.         }
  38.     }
  39. }
  40.  
  41. /* Given a name, find or create an image family with that name. */
  42.  
  43. ImageFamily *
  44. get_imf(name)
  45. char *name;
  46. {
  47.     int i;
  48.     ImageFamily *imf = NULL;
  49.  
  50.     if (name == NULL) return NULL;
  51.     /* Resource names ending in " mask" are parts of other images. */
  52.     if (strcmp(name+strlen(name)-5, " mask") == 0) return NULL;
  53.     if ((imf = find_imf(name)) == NULL) {
  54.         if (numimages >= MAXIMAGES) {
  55.             return NULL;
  56.         }
  57.         if ((imf = (ImageFamily *) xmalloc(sizeof(ImageFamily))) != NULL) {
  58.             init_image_family(imf);
  59.             imf->name = name;
  60.             load_image_family(imf);
  61.             images[numimages++] = imf;
  62.             /* Do some visual feedback periodically. */
  63.             if (numimages % 10 == 0) draw_topline();
  64.         }
  65.     }
  66.     return imf;
  67. }
  68.  
  69. /* Find the image family of the given name, if it exists. */
  70.  
  71. ImageFamily *
  72. find_imf(name)
  73. char *name;
  74. {
  75.     int i;
  76.  
  77.     for (i = 0; i < numimages; ++i) {
  78.         if (strcmp(name, images[i]->name) == 0) return images[i];
  79.     }
  80.     return NULL;
  81. }
  82.  
  83. char *
  84. canonical_color_name(str)
  85. char *str;
  86. {
  87.     return str;
  88. }
  89.  
  90. /* Given a name, find or create an image color with that name. */
  91.  
  92. ImageColor *
  93. get_imc(name)
  94. char *name;
  95. {
  96.     int i;
  97.     ImageColor *imc = NULL;
  98.  
  99.     if (name == NULL) return NULL;
  100.     if ((imc = find_imc(name)) == NULL) {
  101.         if (numcolors >= MAXIMAGES) return NULL;
  102.         if ((imc = (ImageColor *) xmalloc(sizeof(ImageColor))) != NULL) {
  103.             imc->name = canonical_color_name(name);
  104.             imc->r = imc->g = imc->b = 0;
  105.             load_image_color(imc);
  106.             colors[numcolors++] = imc;
  107.             /* Do some visual feedback periodically. */
  108.             if (numcolors % 10 == 0) draw_topline();
  109.         }
  110.     }
  111.     return imc;
  112. }
  113.  
  114. /* Find the image color of the given name, if it exists. */
  115.  
  116. ImageColor *
  117. find_imc(name)
  118. char *name;
  119. {
  120.     int i;
  121.  
  122.     for (i = 0; i < numcolors; ++i) {
  123.         if (colornamecmp(name, colors[i]->name) == 0) return colors[i];
  124.     }
  125.     return NULL;
  126. }
  127.  
  128. colornamecmp(str1, str2)
  129. char *str1, *str2;
  130. {
  131.     while (*str1 != '\0' && *str2 != '\0') {
  132.         if (*str1 == *str2) {
  133.             ++str1;  ++str2;
  134.         } else if (isalpha(*str1) && isalpha(*str2) && tolower(*str1) == tolower(*str2)) {
  135.             ++str1;  ++str2;
  136.         } else if (*str1 == 'a' && *str2 == 'e' && *(str1+1) == 'y' && *(str2+1) == 'y') {
  137.             ++str1;  ++str2;
  138.         } else if (*str1 == 'e' && *str2 == 'a' && *(str1+1) == 'y' && *(str2+1) == 'y') {
  139.             ++str1;  ++str2;
  140. #if 0
  141.         } else if (*str1 == ' ') {
  142.             ++str1;
  143.         } else if (*str2 == ' ') {
  144.             ++str2;
  145. #endif
  146.         } else {
  147.             return *str1 - *str2;
  148.         }
  149.     }
  150.     if (*str1 == '\0') {
  151.         if (*str2 == '\0') {
  152.             return 0;
  153.         } else {
  154.             return 1;
  155.         }
  156.     } else {
  157.         if (*str2 == '\0') {
  158.             return -1;
  159.         } else {
  160.             /* can never happen */
  161.         }
  162.     }
  163. }
  164.  
  165. /* Write all the images into an imf file. */
  166.  
  167. save_imf_file()
  168. {
  169.     int i, n;
  170.     char filename[BUFSIZE];
  171.     Point pnt;
  172.     FILE *fp;
  173.     SFReply reply;
  174.  
  175.     SetPt(&pnt, 100, 100);
  176.     SFPutFile(pnt, "\p", "\plib.imf", nil, &reply);
  177.     if (reply.good) {
  178.         /* Make the location of the file be the current volume. */
  179.         SetVol(reply.fName, reply.vRefNum);
  180.         p2c(((char *) reply.fName), filename);
  181.         SetCursor(*watchcursor);
  182.         if ((fp = fopen(filename, "w")) != NULL) {
  183.             /* Write out the imf forms of all the image families. */
  184.             for (i = 0; i < numimages; ++i) {
  185.                 write_imf(fp, images[i]);
  186.             }
  187.             /* Write out the image colors. */
  188.             for (i = 0; i < numcolors; ++i) {
  189.                 write_imc(fp, colors[i]);
  190.             }
  191.             fclose(